﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;

namespace RaisingForce.Raise.Measures
{
    sealed internal class AngleProvider :
        PositiveDoubleMeasureProvider<IAngleMeasure, AngleProvider>
    {
        #region Constructors

        private AngleProvider()
            : base()
        { }

        #endregion

        #region Public Methods

        [Pure]
        public DoubleMeasureValue<IAngleMeasure> GetNormalized(
            DoubleMeasureValue<IAngleMeasure> value)
        {
            Contract.Requires(!value.IsDefault());
            Contract.Ensures(!value.IsDefault());
            if (this.IsUnknown(value))
            {
                return value;
            }
            double normalizedValue =
                value.InnerMeasure.GetNormalized(value.InnerValue);
            return new DoubleMeasureValue<IAngleMeasure>(
                normalizedValue,
                value.InnerMeasure);
        }

        #endregion

        #region Base Overrides

        protected override IAngleMeasure DefaultMeasure
        {
            get { return AngleMeasures.Degree; }
        }

        protected override IAngleMeasure InvariantMeasure
        {
            get { return AngleMeasures.Radian; }
        }

        protected override string GetString(
            DoubleMeasureValue<IAngleMeasure> value,
            string format,
            IFormatProvider formatProvider)
        {
            IAngleMeasure usingMeasure = AngleMeasures.Degree;
            double valueInDegree = this.Get(value, usingMeasure);
            string valueString =
                this.GetValueString(
                    valueInDegree,
                    format ?? "F0",
                    formatProvider);
            return string.Concat(valueString, usingMeasure.ShortName);
        }

        protected override double EpsilonInvariantValue
        {
            get
            {
                return 1.0d / 3600000.0d; // 1 millisecond in hour
            }
        }

        #endregion
    }
}
